package de.dhbw.asa.controller;

import java.sql.ResultSet;
import java.util.ArrayList;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import de.dhbw.asa.model.Database;
import de.dhbw.asa.model.Product;

@ManagedBean
@RequestScoped
public class ListProductsCtrl extends Database {

	private ArrayList<Product> products;
	private int counter;

	public String forward() {
		counter += 5;

		return null;
	}

	public String backward() {

		counter -= 5;

		if (counter < 0)
			counter = 0;

		return null;
	}

	public ListProductsCtrl() {
		try {
			products = new ArrayList<Product>();
			this.createConnection();
			StringBuffer sbQuery = new StringBuffer();
			sbQuery.append("Select * from Product;");
			ResultSet jResult = this.executeQuery(sbQuery.toString());
			while (jResult.next()) {
				Product jProduct = new Product();
				jProduct.load(jResult.getInt("productNr"));
				products.add(jProduct);
			}
			this.closeConnection();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	

	public ArrayList<Product> getProducts() {
		return products;
	}

	public void setProducts(ArrayList<Product> products) {
		this.products = products;
	}

	public int getCounter() {
		return counter;
	}

	public void setCounter(int counter) {
		this.counter = counter;
	}

}
